﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

using ISS.CaseStudy.BLL;

namespace CaseStudy.AdminPortal
{
    public partial class RelatedInfoManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            loginInfo.Login();

            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void gvRelatedInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "edit")
            {
                Response.Redirect("RelatedInfoDetail.aspx?id=" + e.CommandArgument.ToString() + "&mode=edit");
            }
            else if (e.CommandName.ToLower() == "delete")
            {
                RelatedInformationBLL.Delete(e.CommandArgument.ToString());
                CaseStudyRelatedInformationBLL.DeletedByRelatedId(e.CommandArgument.ToString());

                BindData();
                Response.Redirect(this.Request.Url.ToString());
            }
            else if (e.CommandName.ToLower() == "view")
            {
                Response.Redirect("RelatedInfoDetail.aspx?id=" + e.CommandArgument.ToString() + "&mode=view");
            }
        }

        protected void gvRelatedInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRelatedInfo.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void imgBtnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("RelatedInfoDetail.aspx");
        }

        private void BindData()
        {
            DataSet dataSet = RelatedInformationBLL.GetAll();
            if (dataSet.Tables.Count == 0) return;
            DataTable dataTable = dataSet.Tables[0];

            foreach (DataRow row in dataTable.Rows)
            {
                row["Title"] = HttpUtility.HtmlDecode(row["Title"].ToString());
                row["Url"] = HttpUtility.HtmlDecode(row["Url"].ToString());
            }

            gvRelatedInfo.DataSource = dataTable.DataSet;
            gvRelatedInfo.DataBind();
        }
    }
}
